
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Diagnostics;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Security;
using Swaf.Gui;


namespace MediaCache.Gui
{
    /// <summary>
    /// 
    /// </summary>
    public class MediaDirectoryItemsBizProc : BaseBusinessProcess
    {
		protected static string ss_dirItems="SelectedDirectoryItems";
		protected IBizObjList m_checkedImages = null;

		public MediaDirectoryItemsBizProc(ActiveBizProc procState) : base(procState) { }

		protected override void setupRuleNames()
		{
			base.m_getBizObjListRule = "getMediaItemsFromDirectory";
			base.m_updateBizObjRule = "addPrimaryMedia";
		}

		protected override Privileges CurrentEditPrivilege { get { return CurrentPrincipal.getPrivilege("AppFunctions.MediaCacheManagement"); } }

		protected override object[] getBizObjListRuleParameters(object parentId, IBizObj filterSort)
		{
			if (parentId != null && parentId.ToString().Contains(":")) 
				parentId = parentId.ToString().Replace(':', System.IO.Path.DirectorySeparatorChar);
			string cacheName = (string)m_procState.get("cid");
			if(cacheName == null)
				cacheName = ActiveCompany.get("settings.defaultMediaCatalogName").ToString();
			object[] objs = new object[] { cacheName, MediaCache.ImageListType.filesOnly, parentId };
			return objs;
		}

		public override void bizObjCheckedChanged(IBizObjList uncheckedElements, IBizObjList checkedElements)
		{
			m_checkedImages = checkedElements;
		}

		public override void deleteBizObj()
		{
			updateBizObj(true);
			if (m_checkedImages != null && m_checkedImages.numElements > 0)
			{
				IResults rc = App.ruleMgr.execute("removeImages", ActiveCacheName, m_checkedImages);
				m_checkedImages = null;
				getBizObjList();
				forceParentBizProcUpdates();
				showData(ForceLevel.mustShowForceDataRetrieval);
			}
		}

		protected void addToSelected(object itm)
		{
			selectBizObj(itm as string, this);
			//Swaf.BGuid.Guid articleId = m_procState.get("ArtId") as Swaf.BGuid.Guid;
		}

		public void moveSelectedToDir(object dirName)
		{
			updateBizObj(true);
			if (m_checkedImages != null && m_checkedImages.numElements > 0)
			{
				IResults rc = App.ruleMgr.execute("moveImagesToDirectory", ActiveCacheName, m_checkedImages, dirName);
				m_checkedImages = null;
				getBizObjList();
				forceParentBizProcUpdates();
				showData(ForceLevel.mustShowForceDataRetrieval);
			}
		}

		protected string ActiveCacheName
		{
			get
			{
				string cacheName = (string)m_procState.get("cid");
				if (cacheName == null)
					cacheName = ActiveCompany.get("settings.defaultMediaCatalogName").ToString();
				return cacheName;
			}
		}

		protected void createArticleFromSelected()
		{
		}
		protected override void setupActions()
		{
			base.setupActions();
			Actions.addAction(new GenericAction("AddToSelected", true, (CurrentEditPrivilege > Privileges.Edit), this, new ActionParamTrigger(addToSelected), new object[] { }));
			Actions.addAction(new GenericAction("Move", true, (CurrentEditPrivilege > Privileges.Edit), this, new ActionParamTrigger(moveSelectedToDir), new object[] { }));
			//Actions.addAction(new GenericAction("GenerateChildArticles", true, generateChildArticles, this));
			//Actions.addAction(new GenericAction("RemoveSelected", true, removeSelected, this));
			//Actions.addAction(new GenericAction("SaveSelected", true, saveSelected, this));
			

		}
	}
}
